package ru.papergames.common.netutils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
 * User: troshanin
 * Date: 15.01.2011
 * Time: 0:57:47
 */
public abstract class BaseBufferUtils {
    
    public static void writeC(ByteBuffer buf, final int data) {
        buf.put((byte) data);
    }

    public static void writeShort(ByteBuffer buf, final int value) {
        buf.putShort((short) value);
    }

    public static void writeInt(ByteBuffer buf, final int value) {
        buf.putInt(value);
    }

    public static void writeDouble(ByteBuffer buf, final double value) {
        buf.putDouble(value);
    }

    public static void writeFloat(ByteBuffer buf, final float value) {
        buf.putFloat(value);
    }

    public static void writeLong(ByteBuffer buf, final long value) {
        buf.putLong(value);
    }

    public static void writeBytes(ByteBuffer buf, final byte[] data) {
        buf.put(data);
    }

    public static void writeBool(ByteBuffer buf, final boolean b) {
        buf.put((byte) (b ? 1 : 0));
    }

    public static void writeS(ByteBuffer buf, final String text) {
        if (text != null) {
            final int len = text.length();
            for (int i = 0; i < len; i++) {
                buf.putChar(text.charAt(i));
            }
        }

        buf.putChar('\000');
    }

    public static void writeString(ByteBuffer buf, final String text) {
        if (text != null) {
            byte [] bytes = stringToBytes(text, "UTF8");
            buf.putShort((short) bytes.length);
            buf.put(bytes);
        } else {
            buf.putShort((short) -1);
        }
    }

    private static byte [] stringToBytes(String text, String charset) {
        try {
            return text.getBytes(charset);
        } catch (Exception e) {
            return text.getBytes();
        }
    }

    public static void readBytes(ByteBuffer buf, final byte[] dst) {
        buf.get(dst);
    }

    public static void readBytes(ByteBuffer buf, final byte[] dst, final int offset, final int len) {
        buf.get(dst, offset, len);
    }

    public static int readC(ByteBuffer buf) {
        return buf.get() & 0xFF;
    }

    public static boolean readBool(ByteBuffer buf) {
        return readC(buf) == 1;
    }

    public static int readShort(ByteBuffer buf) {
        return buf.getShort() & 0xFFFF;
    }

    public static int readInt(ByteBuffer buf) {
        return buf.getInt();
    }

    public static long readLong(ByteBuffer buf) {
        return buf.getLong();
    }

    public static float readFloat(ByteBuffer buf) {
        return buf.getFloat();
    }

    public static double readDouble(ByteBuffer buf) {
        return buf.getDouble();
    }

    public static String readS(ByteBuffer buf) {
        StringBuilder res = new StringBuilder();
        byte b;
        while ((b = buf.get()) != 0) {
            char ch = (char) (b & 0xFF);
            res.append(ch);
        }
        return res.toString();
    }

    public static String readS2(ByteBuffer buf) {
        StringBuilder res = new StringBuilder();
        char ch;
        while ((ch = buf.getChar()) != 0) {
            res.append(ch);
        }
        return res.toString();
    }

    public static String readString(ByteBuffer buf) {
        int len = buf.getShort();
        if (len != -1) {
            byte [] bytes = new byte[len];
            buf.get(bytes);
            return bytesToString(bytes, "UTF8");
        } else {
            return null;
        }
    }

    private static String bytesToString(byte [] bytes, String charset) {
        try {
            return new String(bytes, charset);
        } catch (Exception e) {
            return new String(bytes);
        }
    }

}
